'use strict';
/**
 * @fileoverview 开服活动
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var moment = require('moment');
var mongoose = require('mongoose');
var activityMgr = require('../mgr/activityMgr');
var dataApi = require('../libs/dataApi');
var configUtil = require('../util/configUtil');

var _openTime = function () {
    var open_time = configUtil.load('open').open_time;
    if (moment().unix() >= open_time + 24 * 3600 * 7) {
        return 1;
    } else {
        return 0;
    }
};
/**
 * 开服活动
 */
var activitySchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 连续登陆天数
    continuously_login_days: {
        type: Number,
        default: 0
    },

    // 是否第1周
    first_week: {
        type: Number,
        default: 1
    },

    // 上次登录日期
    pre_login_day: {
        type: Number,
        default: 0
    },

    // 成长领取奖励等级列表
    grow_up_received_lv_list: [Number],

    // 冲级奖励领取等级列表
    upgrade_received_lv_list: [Number],

    // 开服狂欢奖励领取天数列表
    open_server_received_day_list: [Number],

    // 取领取连续7天登陆奖励列表
    seven_login_received_day_list: [Number],

    // 城堡等级奖励领取列表
    main_city_received_list: [Number],

    // 7天开服狂欢奖励已领取列表
    carnival_receive_list: [Number],

    // 7天开服狂欢奖励已领取状态
    open_carnival_state: {
        type: Number,
        default: _openTime
    },

    // 当前是否有可领取任务
    now_receive_state: [Number],

    // 下次刷新开服限购次数时间
    next_refresh_limit_buy_tm: {
        type: Number,
        default: 0
    },

    // 开服限购剩余次数
    limit_buy_remain_times: {
        type: Number,
        default: 0
    },

    // 成长基金购买状态
    grow_fund_buy_status: {
        type: Number,
        default: 0
    },

    // 成长基金已领取奖励列表
    grow_fund_receive_list: [Number],
});

activitySchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 是否第1周
 * @return {[type]} [description]
 */
activitySchema.methods.isFirst = function () {
    return this.first_week;
};

/**
 * 任务是否已领取
 * @return {[type]} [description]
 */
activitySchema.methods.isReceiveState = function (carnival_id) {
    return _.indexOf(this.now_receive_state, carnival_id) == -1;
};

/**
 * 设置 当前是否有可领取任务
 * @return {[type]} [description]
 */
activitySchema.methods.addReceiveState = function (carnival_id) {
    this.now_receive_state.push(carnival_id);
};

/**
 * 删除 当前是否有可领取任务
 * @return {[type]} [description]
 */
activitySchema.methods.subReceiveState = function (carnival_id) {
    this.now_receive_state.pull(carnival_id);
};

/**
 * 获取7天开服狂欢已完成列表
 * @return {[type]} [description]
 */
activitySchema.methods.getCarnivalReceive = function () {
    return this.carnival_receive_list;
};

/**
 * 7天开服狂欢奖励领取列表——是否领取过(true: 没有领取过)
 * @return {[type]} [description]
 */
activitySchema.methods.isCarnivalReceive = function (carmoval_id) {
    return _.indexOf(this.carnival_receive_list, carmoval_id) == -1;
};

/**
 * 增加7天开服狂欢奖励记录
 * @return {[type]} [description]
 */
activitySchema.methods.addCarnivalReceive = function (carmoval_id) {
    if (!!this.isCarnivalReceive(carmoval_id)) {
        this.carnival_receive_list.push(carmoval_id);
    }
};

/**
 * 添加累计登陆天数
 * @return {[type]} [description]
 */
activitySchema.methods.addSevenLoginReceivedDays = function (day) {
    if (!this.sevenLoginRewardIsCanReceive(day)) {
        return false;
    }
    this.seven_login_received_day_list.push(day);
    return true;
};

/**
 * 获取累计登陆天数
 * @return {[type]} [description]
 */
activitySchema.methods.getContinuouslyLoginDays = function () {
    return this.continuously_login_days;
};

/**
 * 是否领取奖励天数
 * @return {[type]} [description]
 */
activitySchema.methods.sevenLoginRewardIsCanReceive = function (day) {
    if (_.indexOf(this.seven_login_received_day_list, day) !== -1) {
        return false;
    }
    return true;
};

/**
 * 添加开服狂欢领取奖励天数
 * @return {[type]} [description]
 */
activitySchema.methods.addOpenServerReceivedDays = function (day) {
    if (!this.openServerRewardIsCanReceive(day)) {
        return false;
    }
    this.open_server_received_day_list.push(day);
    return true;
};

/**
 * 是否可以领取奖励
 * @return {[type]} [description]
 */
activitySchema.methods.openServerRewardIsCanReceive = function (day) {
    if (_.indexOf(this.open_server_received_day_list, day) !== -1) {
        return false;
    }
    return true;
};

/**
 * 添加成长奖励领取等级
 * @return {[type]} [description]
 */
activitySchema.methods.addGrowUpReceivedLvList = function (lv) {
    if (!this.growUpRewardIsCanReceive(lv)) {
        return false;
    }
    this.grow_up_received_lv_list.push(lv);
    return true;
};

/**
 * 是否已经领取
 * @return {[type]} [description]
 */
activitySchema.methods.growUpRewardIsCanReceive = function (lv) {
    if (_.indexOf(this.grow_up_received_lv_list, lv) !== -1) {
        return false;
    }
    return true;
};

/**
 * 是否已经领取
 * @return {[type]} [description]
 */
activitySchema.methods.mainCityUpRewardIsCanReceive = function (lv) {
    if (_.indexOf(this.main_city_received_list, lv) !== -1) {
        return false;
    }
    return true;
};

/**
 * 添加冲级奖励领取等级
 * @return {[type]} [description]
 */
activitySchema.methods.addMainCityUpReceivedLvList = function (lv) {
    if (!this.mainCityUpRewardIsCanReceive(lv)) {
        return false;
    }
    this.main_city_received_list.push(lv);
    return true;
};

/**
 * 添加冲级奖励领取等级
 * @return {[type]} [description]
 */
activitySchema.methods.addLevelReceivedLvList = function (lv) {
    if (!this.levelRewardIsCanReceive(lv)) {
        return false;
    }
    this.upgrade_received_lv_list.push(lv);
    return true;
};

/**
 * 是否已经领取
 * @return {[type]} [description]
 */
activitySchema.methods.levelRewardIsCanReceive = function (lv) {
    if (_.indexOf(this.upgrade_received_lv_list, lv) !== -1) {
        return false;
    }
    return true;
};

/**
 * 添加到已领取成长基金奖励信息列表中
 */
activitySchema.methods.addGrowFundReceiveLevel = function (level) {
    if (_.indexOf(this.grow_fund_receive_list, level) >= 0) {
        return false;
    }
    this.grow_fund_receive_list.push(level);

    return true;
};

/**
 * 累计登陆天数
 * @return {[type]} [description]
 */
activitySchema.methods.addContinuouslyLoginDays = function () {
    var now_day = moment().startOf('day').unix();
    if (now_day === this.pre_login_day) {
        // 当天登录已处理
        return;
    }

    if (this.continuously_login_days >= 7) {
        this.pre_login_day = now_day;
        if (this.seven_login_received_day_list.length === 7) {
            // 所有奖励领取，则开启新的周期
            this.continuously_login_days = 1;
            this.seven_login_received_day_list = [];
            this.first_week = 0;
        }
        return;
    }

    if (this.pre_login_day === now_day - 86400) {
        // 前一天有登录，则继续累加
        this.continuously_login_days++;
    } else {
        // 前一天未登录，则重新从第1天开始
        this.continuously_login_days = 1;
    }
    this.pre_login_day = now_day;
};

/**
 * 减少开服限购次数
 * @return {[type]} [description]
 */
activitySchema.methods.decLimitBuyTimes = function () {
    var self = this;
    if (self.limit_buy_remain_times < 1) {
        return false;
    }

    self.limit_buy_remain_times--;
    return true;
};

/**
 * 设置成长基金购买状态
 */
activitySchema.methods.setGrowFundBuyStatus = function () {
    if (this.grow_fund_buy_status == 1) {
        return false;
    }

    this.grow_fund_buy_status = 1;
    return true;
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
activitySchema.methods.getLastChange = function () {
    var change_info = this.toJSON();
    change_info.open_game_time = configUtil.getOpenGameTime();
    change_info.open_game_date = configUtil.getOpenGameDate();
    return change_info;
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
activitySchema.methods.push = function () {
    this.pushModify();
};

activitySchema.methods.pushModify = function () {
    activityMgr.sendActivityChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send activity message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!activitySchema.options.toJSON) {
    activitySchema.options.toJSON = {};
}
/* jshint unused:false */
activitySchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Activity', activitySchema);
